# 定义获取驱动对象的类
import json

from selenium import webdriver
from selenium.webdriver.common.by import By


class UtilsDriver:
    _driver = None
    # 获取浏览器驱动对象
    @classmethod
    def get_driver(cls):
        if cls._driver is None:
            print("创建驱动")
            cls._driver = webdriver.Chrome()
            cls._driver.maximize_window()
            cls._driver.get("http://localhost/")
            cls._driver.implicitly_wait(10)
        return cls._driver

    # 退出浏览器驱动
    @classmethod
    def quit_driver(cls):
        if cls._driver is not None:
            cls.get_driver().quit()
            cls._driver = None  # 因为cls._driver退出之后，值不为none，只是一串失效的session，那么第二次执行测试文件就无法创建驱动对象


# 定义获取提示信息的方法
def get_msg():
    msg = UtilsDriver.get_driver().find_element(By.CSS_SELECTOR, ".layui-layer-content").text
    return msg


# 定义获取用例数据的方法
def get_cast_data(filename):
    with open(filename, encoding='utf-8') as f:
        file_data = json.load(f)
    case_data = []  # 定义空列表
    for i in file_data.values():
        case_data.append(tuple(i.values()))
    return case_data



# get_cast_data("./data/test_login_data2.json")